package masterjava.common.convertion;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.GenericConversionService;

/**
 * User: GKislin
 * Date: 20.02.2011
 */
public class ConverterUtils {

    private static ConversionService SPRING_CONVERSION;

    public void setConversionService(GenericConversionService conversionService) {
        SPRING_CONVERSION = conversionService;
    }

    @SuppressWarnings("unchecked")
    public static <T> T toValue(String property, Class<T> targetType) {
        return (targetType == String.class) ? (T) property : SPRING_CONVERSION.convert(property, targetType);
    }

    public static <T> String toString(T value) {
        return SPRING_CONVERSION.convert(value, String.class);
    }
}
